<?php

namespace App\Models;

use App\Models\BaseModel;

use SmallRuralDog\Admin\Traits\ModelTree;

class GoodsClass extends BaseModel
{
    use ModelTree;

    protected $casts = [
        'created_at' => 'Y-m-d H:i:s',
        'updated_at' => 'Y-m-d H:i:s',
    ];

    public function children()
    {
        return $this->hasMany(get_class($this), 'parent_id')->orderBy('order');
    }


    /**
     * 商品分类列表
     *
     */
    public static function getCategroyList()
    {
        $data = self::where('status', 1)->orderBy('order', 'ASC')->orderBy('id', 'ASC')->get();

        if ($data)
        {
            $data_ = $data->toArray();

            $data = [];
            foreach ($data_ as $key=>$val)
            {
                if (!$val) continue;

                if ($val['parent_id']==0)
                {
                    $data[$val['id']] = $val;
                }
                else
                {
                    $data[$val['parent_id']]['children'][] = $val;
                }
            }

            $data = array_values($data);
        }

        return self::formatBody(['list' => $data]);
    }
}
